/**
 * <p>Title: AuthoritySvc</p>
 * <p>Description: The Implement of AuditTrialSvc</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Huang Zhongwei
 * @version 1.0
 * <pre>Histroy:
 *       2016-10-10  Huang Zhongwei  Create
 *</pre>
 */
package cn.jhg.account.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.jhg.account.dao.AuditTrailMetadataDao;
import cn.jhg.account.entity.AuditTrailRecord;
import cn.jhg.account.persist.AuditTrailRecordDao;
import cn.jhg.account.service.SignatureSvcImpl;
import cn.jhg.account.util.SequenceProducer;
import cn.jhg.account.util.ServiceResult;


@Service("auditTrailSvc")
@Transactional
public class AuditTrailSvcImpl implements AuditTrailSvc {
	@Resource
	private AuditTrailRecordDao auditTrailRecordDao;
	@Resource
	private SequenceProducer sequenceProducer;
	@Resource
	private AuditTrailMetadataDao auditTrailMetadataDao;
	Logger logger=LoggerFactory.getLogger(SignatureSvcImpl.class);
	
	/**
	 * 添加审计追踪记录
	 * @param record
	 * @return 错误代码
	 */
	@Override
	public ServiceResult<Integer> addRecord(AuditTrailRecord record) {
		ServiceResult<Integer> result=new ServiceResult<Integer>();
		try{
			int id=sequenceProducer.nextval("t_ibcp_audit_trail_record");
			ObjectId objectId=new ObjectId();
			record.setMetadata(objectId.toString());
			Document meta=record.getMeta();
			auditTrailMetadataDao.addMetadata(objectId, meta);
			
			record.setId(id);
			record.setTimestamp(new Timestamp(System.currentTimeMillis()));
			auditTrailRecordDao.addRecord(record);
			result.setErrorCode(0);
			result.setData(id);
			logger.info("[INF221722140] addRecord成功！"+result);
			return result;
		}catch(Exception e){
			result.setErrorCode(220082519);
			logger.error("[ERR220082519] 添加审计追踪记录失败"+e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return result;
		}
	}
	
	/**
	 * 查找审计追踪记录
	 * @param id
	 * @return 错误代码,审计追踪记录
	 */
	@Override
	public ServiceResult<AuditTrailRecord> queryRecordById(int id) {
		ServiceResult<AuditTrailRecord> result=new ServiceResult<AuditTrailRecord>();
		try{
			AuditTrailRecord record=auditTrailRecordDao.findRecordById(id);
			if(record==null){
				result.setErrorCode(219319837);//此ID的审计追踪不存在
				logger.error("[ERR219319837] 此ID的审计追踪不存在");
			}else{
				String metadata=record.getMetadata();
				ObjectId objectId=new ObjectId(metadata);
				Document document=auditTrailMetadataDao.queryMetadata(objectId);
				record.setMeta(document);
				
				result.setErrorCode(0);//查询成功
				result.setData(record);
				logger.info("[INF221722427] queryRecordById成功！"+result);
			}
			return result;
		}catch(Exception e){
			result.setErrorCode(220082987);
			logger.error("[ERR220082987] 根据ID查询审计追踪失败"+e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return result;
		}
	}
	
	/**
	 * 查找审计追踪记录ID
	 * @param begin	起始时间
	 * @param end	结束时间
	 * @param tid	审计追踪类型
	 * @param otid	对象类型
	 * @param uid	用户 id
	 * @param lid	位置 id
	 * @return 错误代码,审计追踪记录ID
	 */
	public ServiceResult<List<Integer>> findRecord(Timestamp begin, Timestamp end, int tid, int otid, int uid,
			int lid) {
		ServiceResult<List<Integer>> result=new ServiceResult<List<Integer>>();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("lid",lid);
		map.put("tid",tid);
		map.put("uid",uid);
		map.put("otid",otid);
		map.put("begin",begin);
		map.put("end",end);
		try{
		List<Integer> records=auditTrailRecordDao.findRecords(map);
		result.setData(records);
		result.setErrorCode(0);
		logger.info("[INF221722446] findRecord成功！"+result);
		return result;
		}catch(Exception e){
			result.setErrorCode(220083074);
			logger.error("[ERR220083074] 查询审计追踪ID失败"+e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return result;
		}
	}
	

}
